//
//  MineViewController.m
//  ShouYi
//
//  Created by yurui on 16/2/18.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "MineViewController.h"
#import "CustomCollectionViewCell.h"
#import "HeaderView.h"
#import "SettingViewController.h"
#import "MyTeamViewController.h"
#import "LoginViewController.h"
#import "MyShopViewController.h"
#import "MyListViewController.h"
#import "MyRenWuViewController.h"
#import "GoodsManageViewController.h"
#import "MarketViewController.h"
@interface MineViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIAlertViewDelegate>
@property(nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) UIView *groundView;// 头部存放个人信息的view
@property(nonatomic,strong) UIView *secondView; // 存放 团队 收入 销量的View
@property(nonatomic,strong) UIButton *goBT;

@property(nonatomic,strong) UILabel *userName;
@property(nonatomic,strong) UIView *myTeamView;// 我的团队
@property(nonatomic,strong) UILabel *myTeam;
@property(nonatomic,strong) UILabel *myTeamD;
@property(nonatomic,strong) UIView *inComeView;// 月收入
@property(nonatomic,strong) UILabel *inCome;
@property(nonatomic,strong) UILabel *inComeD;
@property(nonatomic,strong) UIView *salesView;// 月销量
@property(nonatomic,strong) UILabel *sales;
@property(nonatomic,strong) UILabel *salesD;

@property(nonatomic,strong) UIImageView *userImageV;
@property(nonatomic,strong) UILabel *userID;
@property(nonatomic,strong) UIImageView *goImage;
@property(nonatomic,strong) UITapGestureRecognizer *tapGesture;

@property(nonatomic,strong) UIView *bigview;
@property(nonatomic,strong) AppDelegate *delegate;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kViewBackColor;
   self.navigationItem.title = @"我的";
    self.delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    _delegate.isLogin = [[kUserDefault objectForKey:@"isLogin"] intValue];
    _delegate.isThirdLogin = [[kUserDefault objectForKey:@"thirdLogin"] intValue];
    [self configerCollectionView];
    //[self addHeaderView];
    
}

- (void)loginOutButton{
    _bigview = [[UIView alloc]initWithFrame:self.view.frame];
    _bigview.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_bigview];
    _bigview.alpha = 0.5;
    UIAlertView *loginOutAView = [[UIAlertView alloc]initWithTitle:@"确定退出登录?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [loginOutAView show];
}

/*
- (void)viewWillAppear:(BOOL)animated{
//    _delegate.isLogin = [[kUserDefault objectForKey:@"isLogin"] intValue];
//    NSLog(@"isLogin = %d",_delegate.isLogin);
//    _delegate.isThirdLogin = [[kUserDefault objectForKey:@"thirdLogin"] intValue];
//    if (_delegate.isLogin == 1 || _delegate.isThirdLogin == 1) {
//        //[self addLoginInSubVies];
//        //[_loginStyle removeFromSuperview];
//    }
//    else if (_delegate.isLogin == 0 ){
//        //[self addLoginOutSubViews];
//       // [_userName removeFromSuperview];
//    }
//    else if (_delegate.isThirdLogin == 0){
//        //[self addLoginOutSubViews];
//        //[_userName removeFromSuperview];
//    }
}
//- (void)viewDidDisappear:(BOOL)animated{
//    [self.userID removeFromSuperview];
//    [self.userName removeFromSuperview];
//    [self.userImageV removeFromSuperview];
//}

*/
// 轻拍手势响应方法
- (void)tapGestureAction:(UITapGestureRecognizer *)sender{
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

// 配置collectionView
- (void)configerCollectionView{
     UICollectionViewFlowLayout *layout=[[ UICollectionViewFlowLayout alloc ] init ];
    layout.itemSize = CGSizeMake((kWidth - 6) / 4 , 90);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = kViewBackColor;
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册页眉
    [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}
#pragma mark UICollectionViewDataSource 和 UICollectionViewDelgeate代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 12;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_01.png"];
    }else if (indexPath.row == 1) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_02.png"];
    }else if (indexPath.row == 2) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_03.png"];
    }else if (indexPath.row == 3) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_04.png"];
    }else if (indexPath.row == 4) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_05.png"];
    }else if (indexPath.row == 5) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_06.png"];
    }else if (indexPath.row == 6) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_07.png"];
    }else if (indexPath.row == 7) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_08.png"];
    }else if (indexPath.row == 8) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_09.png"];
    }else if (indexPath.row == 9) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_10.png"];
    }else if (indexPath.row == 10) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_11.png"];
    }else if (indexPath.row == 11) {
        cell.imageView.image = [UIImage imageNamed:@"icon_wd_12.png"];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        self.tabBarController.selectedIndex = 2;
    }else if (indexPath.row == 1) {
        self.hidesBottomBarWhenPushed = YES;
        GoodsManageViewController *goodsVC = [[GoodsManageViewController alloc]init];
        [self.navigationController pushViewController:goodsVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 2) {

        self.hidesBottomBarWhenPushed = YES;
        MarketViewController *MarketVC = [[MarketViewController alloc]init];
        [self.navigationController pushViewController:MarketVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        
    }else if (indexPath.row == 3) {
        self.hidesBottomBarWhenPushed = YES;
        MyShopViewController *shopVC = [[MyShopViewController alloc]init];
        [self.navigationController pushViewController:shopVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 4) {
        MyListViewController *listVC =[[MyListViewController alloc]init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:listVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 5) {
        MyTeamViewController *teamVC = [[MyTeamViewController alloc]init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:teamVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 6) {
        MyRenWuViewController *renWuVC = [[MyRenWuViewController alloc]init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:renWuVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
        
    }else if (indexPath.row == 7) {
        self.hidesBottomBarWhenPushed = YES;
        WebViewController *webVC = [[WebViewController alloc] init];
        webVC.webUrl = [NSString stringWithFormat:kErWeiMaUrl,[kUserDefault objectForKey:@"CFUUID"]];
        webVC.textTitle =  @"邀请二维码";
        [self.navigationController pushViewController:webVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;

    }else if (indexPath.row == 8) {

    }else if (indexPath.row == 9) {
        self.tabBarController.selectedIndex = 3;
    }else if (indexPath.row == 10) {
        self.hidesBottomBarWhenPushed = YES;
        WebViewController *webVC = [[WebViewController alloc] init];
        webVC.webUrl = [NSString stringWithFormat:kYingXiongBangUrl,[kUserDefault objectForKey:@"CFUUID"]];
        webVC.textTitle = @"英雄榜";
        [self.navigationController pushViewController:webVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 11) {
        self.hidesBottomBarWhenPushed = YES;
        WebViewController *webVC = [[WebViewController alloc] init];
        webVC.webUrl = [NSString stringWithFormat:kEdgMyMessageUrl,[kUserDefault objectForKey:@"CFUUID"]];
        webVC.textTitle =  @"设置";
        [self.navigationController pushViewController:webVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
}
//设置分区的页眉（区头）返回对应的视图对象，需要重用以及自定义页眉视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //如果是为页眉设置自定义视图
    if (kind == UICollectionElementKindSectionHeader) {
        HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        //为页眉上的控件赋
        headerView.backgroundColor = kViewBackColor;
        
        // 添加头部 登陆视图
        self.groundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 85)];
        self.groundView.backgroundColor = [UIColor whiteColor];
        // 用户头像
        self.userImageV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 65, 65)];
        _userImageV.image = [UIImage imageNamed:@"img_touxxxxx.png"];
        [self.groundView addSubview:_userImageV];
        //userName
        self.userName = [[UILabel alloc]initWithFrame:CGRectMake(85, 20, 150, 20)];
        _userName.font = [UIFont systemFontOfSize:14];
        [self.groundView addSubview:_userName];
        //userID
        self.userID = [[UILabel alloc]initWithFrame:CGRectMake(85, 45, 150, 20)];
        _userID.font = [UIFont systemFontOfSize:14];
        [self.groundView addSubview:_userID];
        // “>” 图标
        self.goImage = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth - 25, 35, 10, 15)];
        self.goImage.image = [UIImage imageNamed:@"icon_qt.png"];
        [self.groundView addSubview:_goImage];
        [headerView addSubview:_groundView];
        // 类似于手势的bt
        self.goBT = [UIButton buttonWithType:UIButtonTypeCustom];
        self.goBT.frame = self.groundView.frame;
        self.goBT.backgroundColor = [UIColor clearColor];
        [self.goBT addTarget:self action:@selector(goToAnotherC) forControlEvents:UIControlEventTouchUpInside];
        [self.groundView addSubview:_goBT];
        
        
        //
        self.secondView = [[UIView alloc]initWithFrame:CGRectMake(0, 95, kWidth, 65)];
        self.secondView.backgroundColor = [UIColor whiteColor];
        [headerView addSubview:_secondView];
        
        //团队
        self.myTeamView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth / 3, 65)];
        self.myTeamView.backgroundColor = [UIColor whiteColor];
        [self.secondView addSubview:_myTeamView];
        //
        self.myTeam = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, kWidth / 3, 15)];
        self.myTeam.text = @"0";
        self.myTeam.textAlignment = NSTextAlignmentCenter;
        [self.myTeamView addSubview:_myTeam];
        //
        self.myTeamD = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, kWidth / 3, 15)];
        self.myTeamD.text = @"我的团队";
        self.myTeamD.font = [UIFont systemFontOfSize:12];
        self.myTeamD.textAlignment = NSTextAlignmentCenter;
        [self.myTeamView addSubview:_myTeamD];
        
        //月收入
        self.inComeView = [[UIView alloc]initWithFrame:CGRectMake(kWidth / 3, 0, kWidth / 3, 65)];
        self.inComeView.backgroundColor = [UIColor whiteColor];
        [self.secondView addSubview:_inComeView];
        //
        self.inCome = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, kWidth / 3, 15)];
        self.inCome.text = @"0";
        self.inCome.textAlignment = NSTextAlignmentCenter;
        [self.inComeView addSubview:_inCome];
        //
        self.inComeD = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, kWidth / 3, 15)];
        self.inComeD.text = @"本月收入";
        self.inComeD.font = [UIFont systemFontOfSize:12];
        self.inComeD.textAlignment = NSTextAlignmentCenter;
        [self.inComeView addSubview:_inComeD];
        
        //月销量
        self.salesView = [[UIView alloc]initWithFrame:CGRectMake(kWidth * 2/ 3, 0, kWidth / 3, 65)];
        self.salesView.backgroundColor = [UIColor whiteColor];
        [self.secondView addSubview:_salesView];
        //
        self.sales = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, kWidth / 3, 15)];
        self.sales.text = @"0";
        self.sales.textAlignment = NSTextAlignmentCenter;
        [self.salesView addSubview:_sales];
        //
        self.salesD = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, kWidth / 3, 15)];
        self.salesD.text = @"本月销量";
        self.salesD.font = [UIFont systemFontOfSize:12];
        self.salesD.textAlignment = NSTextAlignmentCenter;
        [self.salesView addSubview:_salesD];
        
        [self configerHeaderView];
        
        return headerView;
        
    }else{
        return nil;
    }
}

- (void)goToAnotherC{
    if (_delegate.isLogin == 0 && _delegate.isThirdLogin == 0){
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
    }else{
        self.hidesBottomBarWhenPushed = YES;
        WebViewController *webVC = [[WebViewController alloc] init];
        webVC.webUrl = [NSString stringWithFormat:kEdgMyMessageUrl,[kUserDefault objectForKey:@"CFUUID"]];
        webVC.textTitle =  @"我的资料";
        [self.navigationController pushViewController:webVC animated:YES];
        self.hidesBottomBarWhenPushed = NO;

    }
}
//headerView 高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (collectionView == self.collectionView) {
        
        if (section == 0) {
            
            return CGSizeMake(kWidth,170);
        }else {
            return CGSizeMake(kWidth, 0);
        }
    }
    
    return CGSizeMake(0,0);
}



#pragma mark UIAlertView --- 代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        if (_delegate.isLogin == 1){
            NSString *mobile = [kUserDefault objectForKey:@"phoneNum"];
            NSString *uuid = [kUserDefault objectForKey:@"CFUUID"];
            NSDictionary *parmeters = @{@"mobile":mobile,@"uuid":uuid};
            [LORequestManger POST:@"http://wx1.01zg.com/plugins/app/chaozan/main.php?action=userlogout&sid=2216&mobile=&uuid=" params:parmeters success:^(id response) {
                _delegate.isLogin = 0;
                NSString *login = [NSString stringWithFormat:@"%d",self.delegate.isLogin];
                [kUserDefault setObject:login forKey:@"isLogin"];
                [_bigview removeFromSuperview];
                NSLog(@"注销");
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
            }];
            }
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [self configerHeaderView];
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.userID removeFromSuperview];
    [self.userName removeFromSuperview];
    [self.userImageV removeFromSuperview];
}


- (void)configerHeaderView{
    _delegate.isLogin = [[kUserDefault objectForKey:@"isLogin"] intValue];
    NSLog(@"isLogin = %d",_delegate.isLogin);
    _delegate.isThirdLogin = [[kUserDefault objectForKey:@"thirdLogin"] intValue];
    //userName
    self.userName = [[UILabel alloc]initWithFrame:CGRectMake(85, 20, 150, 20)];
    _userName.font = [UIFont systemFontOfSize:14];
    [self.groundView addSubview:_userName];
    //userID
    self.userID = [[UILabel alloc]initWithFrame:CGRectMake(85, 45, 150, 20)];
    _userID.font = [UIFont systemFontOfSize:14];
    [self.groundView addSubview:_userID];
    // 用户头像
    self.userImageV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 65, 65)];
    _userImageV.image = [UIImage imageNamed:@"img_touxxxxx.png"];
    [self.groundView addSubview:_userImageV];
    
    if (_delegate.isLogin == 0 && _delegate.isThirdLogin == 0) {
        self.userID.text = @"未登录";
        self.userID.frame = CGRectMake(85, 35, 100, 20);
        self.userName.text = nil;
        self.userImageV.image = [UIImage imageNamed:@"icon_user.png"];
    } else if (_delegate.isLogin == 0 && _delegate.isThirdLogin == 1){
        self.userName.text = [kUserDefault objectForKey:@"wxname"];
        self.userID.text = [NSString stringWithFormat:@"账号：%@",[kUserDefault objectForKey:@"wxname"]];
        self.userImageV.image = nil;
    } else if (_delegate.isLogin == 1 && _delegate.isThirdLogin == 0){
        self.userID.text = [NSString stringWithFormat:@"账号：%@",[kUserDefault objectForKey:@"phoneNum"]];
        self.userName.text = [NSString stringWithFormat:@"CZ%@",[kUserDefault objectForKey:@"phoneNum"]];
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
